package com.wxzz.elearing.course.api.admin;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.course.dto.*;
import com.wxzz.elearing.course.entity.CurriculumSectionTopic;
import com.wxzz.elearing.course.entity.StudyPlan;
import com.wxzz.elearing.course.entity.TrainSurvey;
import com.wxzz.elearing.course.service.ITrainSurveyService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/train-survey")
@Api(tags = "培训调查(后台)")
public class TrainSurveyController {

    @Autowired
    private ITrainSurveyService trainSurveyService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "新增或修改培训调查", notes = "新增或修改培训调查", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveTrainSurvey", method = RequestMethod.POST)
    public ReturnVO saveTrainSurvey(@RequestHeader String token, @RequestBody TrainSurvey trainSurvey) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        trainSurvey.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        return trainSurveyService.saveTrainSurvey(trainSurvey);
    }

    @ApiOperation(value = "培训调查列表", notes = "培训调查列表", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listTrainSurvey", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<TrainSurvey>>> listTrainSurvey(@RequestHeader String token, @RequestBody TrainSurveyDto trainSurveyDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return trainSurveyService.listTrainSurvey(jsonObject.getLong("enterpriseId"), trainSurveyDto);
    }

    @ApiOperation(value = "培训调查列表", notes = "培训调查列表", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteTrainSurvey/{trainSurveyId}", method = RequestMethod.POST)
    public ReturnVO deleteTrainSurvey(@PathVariable Long trainSurveyId) {
        return trainSurveyService.deleteTrainSurvey(trainSurveyId);
    }

    @ApiOperation(value = "根据id查询培训调查详情", notes = "根据id查询培训调查详情", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectTrainSurveyDetail/{trainSurveyId}", method = RequestMethod.POST)
    public ReturnVO<TrainSurvey> selectTrainSurveyDetail(@PathVariable Long trainSurveyId) {
        return trainSurveyService.selectTrainSurveyDetail(trainSurveyId);
    }

    @ApiOperation(value = "分配考试人", notes = "分配考试人", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveWxUserTrainSurvey", method = RequestMethod.POST)
    public ReturnVO saveWxUserTrainSurvey(@RequestBody WxUserTrainSurveyInsDto wxUserTrainSurveyInsDto) {
        return trainSurveyService.saveWxUserTrainSurvey(wxUserTrainSurveyInsDto);
    }

    @ApiOperation(value = "根据id查询培训调查人员", notes = "根据id查询培训调查人员", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectWxUserTrainSurveyByTrainSurveyId", method = RequestMethod.POST)
    public ReturnVO selectWxUserTrainSurveyByTrainSurveyId(@RequestBody WxUserTrainSurveyDto wxUserTrainSurveyDto) {
        return trainSurveyService.selectWxUserTrainSurveyByTrainSurveyId(wxUserTrainSurveyDto);
    }


    @ApiOperation(value = "查询当前用户培训调查(后台)", notes = "查询当前用户培训调查(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listTrainSurveyByWxUserId", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<TrainSurvey>>> listTrainSurveyByWxUserId(@RequestBody TrainSurveyByWxUserDto trainSurveyByWxUserDto) {
        return trainSurveyService.listTrainSurveyByWxUserId(trainSurveyByWxUserDto.getWxUserId(), trainSurveyByWxUserDto);
    }

    @ApiOperation(value = "根据用户id列出培训调查题目记录(后台)", notes = "根据用户id列出培训调查题目记录(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listTrainSurveyByTrainSurveyHistory/{wxUserId}/{trainSurveyId}", method = RequestMethod.POST)
    public ReturnVO<List<CurriculumSectionTopic>> listTrainSurveyByTrainSurveyHistory(@PathVariable Long wxUserId, @PathVariable Long trainSurveyId) {
        return trainSurveyService.listTrainSurveyByTrainSurveyHistory(wxUserId, trainSurveyId);
    }

    @ApiOperation(value = "培训调查题导入(后台)", notes = "培训调查题导入(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "importTopicSection/{trainSurveyId}", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<StudyPlan>>> importTopicSection(@RequestHeader String token, @PathVariable Long trainSurveyId, @RequestParam("file")  MultipartFile file) {
        return trainSurveyService.importTopicSection(token, trainSurveyId,file);
    }


}
